﻿#pragma execution_character_set("utf-8")
#include "timeline.h"

#include "qpainter.h"
#include "qevent.h"
#include "qlayout.h"
#include "qlabel.h"
#include "qdebug.h"
#include "qpushbutton.h"
#include <QResizeEvent>
#include <QWheelEvent>
#include <QHoverEvent>
#include <QFont>

VideoTime::VideoTime(QWidget *parent)
    : QWidget(parent)
    , textColor_(QColor(255, 255, 255))
    , bgColor_(QColor(60, 60, 60))
{
    setAttribute(Qt::WA_Hover,true);
}

void VideoTime::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);

    //绘制背景
    drawBg(&painter);

    //绘制标尺
    drawRuler(&painter);
}

void VideoTime::wheelEvent(QWheelEvent *event)
{
    if (event->angleDelta().y() > 0 && scaleFactor_ < 10.0) {
        scaleFactor_ += 0.1;
        update();
    }
    else if (scaleFactor_ > 1.0){
        scaleFactor_ -= 0.1;
        update();
    }

    if (scaleFactor_ > 2.0)
    {
        timeType_ = static_cast<TIMETYPE>(timeType_ | TIME_HOUR_MINUTE_5);
    }
    else if (timeType_ & TIME_HOUR_MINUTE_5)
    {
        timeType_ = static_cast<TIMETYPE>(timeType_ ^ TIME_HOUR_MINUTE_5);
    }

    if (scaleFactor_ > 7.0)
    {
        timeType_ = static_cast<TIMETYPE>(timeType_ | TIME_HOUR_MINUTE_1);
    }
    else if (timeType_ & TIME_HOUR_MINUTE_1)
    {
        timeType_ = static_cast<TIMETYPE>(timeType_ ^ TIME_HOUR_MINUTE_1);
    }
    event->accept();
}

void VideoTime::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton) {
        int diffX = (event->pos() - startMovePos_).x();
        if (diffX > 0 && diffX % 60 == 0 && currentStartTime_ > startTime_ ) // 时间向前
        {
            currentStartTime_ -= 1800;
            update();
        }
        else if ( diffX < 0 && diffX % 60 == 0) // 时间向后
        {
            if (endTime_ - currentEndTime_ > 3600) {
                currentStartTime_ += 1800;
            }
            update();
        }
    }

    event->accept();
}

void VideoTime::mousePressEvent(QMouseEvent *event)
{
    startMovePos_ = event->pos();
    event->accept();
}

void VideoTime::mouseReleaseEvent(QMouseEvent *event)
{
    startMovePos_.~QPoint();
    event->accept();
}

bool VideoTime::event(QEvent *event)
{
    switch (event->type())
    {
    case QEvent::HoverMove:
        event->accept();
        hoverMoveEvent(dynamic_cast<QHoverEvent*>(event));
        return true;
        break;
    default:
        break;
    }

    return QWidget::event(event);
}

void VideoTime::hoverMoveEvent(QHoverEvent *event)
{
    Q_UNUSED(event)
}

void VideoTime::drawBg(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(bgColor_);
    painter->drawRect(rect());
    painter->restore();
}

static const quint8 hourLineLen = 15;   // 小时线高度
static const quint8 minuteLineLen = 5; // 分钟线高度

void VideoTime::drawRuler(QPainter *painter)
{
    painter->save();
    painter->setPen(textColor_);

    double initX = 0;
    double initBottomY = height();
    const quint16 timeInteral = 60;
    for (quint32 time = currentStartTime_; time <= endTime_; time += timeInteral)
    {
        if ((time - currentStartTime_) * secondStep_ * scaleFactor_ > width())
        {
            currentEndTime_ = time - timeInteral;
            break;
        }

        if (time % 3600 == 0)  // 1小时
        {
            int startPtX = initX + (time - currentStartTime_) * secondStep_ * scaleFactor_;
            painter->drawLine(QPoint(startPtX, initBottomY), QPoint(startPtX, initBottomY - hourLineLen));

            QString text = QString("%1").arg(time / 3600, 2, 10, QChar('0'));
            QFont font = painter->font();
            font.setPixelSize(10);
            painter->setFont(font);
        #if (QT_VERSION <= QT_VERSION_CHECK(5,11,0))
            int fontWidth = painter->fontMetrics().width(text);
        #else
            int fontWidth = painter->fontMetrics().horizontalAdvance(text);
        #endif
            if (time == currentStartTime_)
            {
                painter->drawText(initX, initBottomY - hourLineLen - 2, text);
            }
            else if (startPtX + fontWidth > this->width())
            {
                painter->drawText(startPtX -  fontWidth, initBottomY - hourLineLen - 2, text);
            }
            else
            {
                painter->drawText(startPtX - fontWidth / 2, initBottomY - hourLineLen - 2, text);
            }
        }
        else if (time % 300 == 0 && timeType_ & TIME_HOUR_MINUTE_5) // 5分钟
        {
            int startPtX = initX + (time - currentStartTime_) * secondStep_ * scaleFactor_;
            painter->drawLine(QPoint(startPtX, initBottomY), QPoint(startPtX, initBottomY - minuteLineLen - minuteLineLen));
        }
        else if (time % 60 == 0 && timeType_ & TIME_HOUR_MINUTE_1) // 1分钟
        {
            int startPtX = initX + (time - currentStartTime_) * secondStep_ * scaleFactor_;
            painter->drawLine(QPoint(startPtX, initBottomY), QPoint(startPtX, initBottomY - minuteLineLen));
        }
    }

    painter->restore();
}
